package docmodel;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 
 * @author andrea
 *
 */
public class XDP_Reader 
{
	DOMParser parser;
	static Document dom;

	private XDP_Liste liste = new XDP_Liste();
	
	public XDP_Liste getListe() {
		return liste;
	}

	/**
	 * 
	 * @param filename
	 */
	public XDP_Reader (String filename) 
	{
		parser = new DOMParser();
	    
		try 
		{
			parser.parse(filename);
		} 
	    catch (SAXException e) {
			e.printStackTrace();
		}
		catch (IOException e){
			e.printStackTrace();
		}
	    
	    dom = parser.getDocument(); 
	}
	
	/**
	 * 
	 * @return
	 */
	public String run () 
	{
		scorriNodo(dom.getDocumentElement());
		
		return "message";
	}
	
	/**
	 * 
	 * @param theNode
	 */
	public void scorriNodo  (Node theNode) 
	{
		NodeList children = theNode.getChildNodes();
	    
		stampaNodo(theNode);
	    
	    for (int i = 0; i < children.getLength(); i++) 
	    {
	    	Node aNode = children.item(i);
	    	
	    	if (aNode.hasChildNodes())
	    		scorriNodo(aNode);
	    	else 
	    	{
	    		String name = aNode.getNodeName();
	    		String value = aNode.getNodeValue(); 
	    		
	    		stampaNodo(name,value);
	    		
	    		if( is_textNode(aNode) ) 
	    		{	
	    			cercaMarker(aNode);	
	    			//System.out.println("nodo testo TROVATO");
	    		}
	    	}
	    }
	}
	
    /**
     * 
     * @param aNode
     */
	private static void stampaNodo(Node aNode)
	{
	    
	    String name = aNode.getNodeName();
	    String value = aNode.getNodeValue(); 
	    
	    System.out.println(name + "," + value);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	private void stampaNodo(String name, String value)
	{
	    
	    System.out.println(name + "," + value);
	}
	
	
    /**
     * 
     * @param aNode
     */
	private boolean is_textNode(Node aNode)
	{
		int tipoNodo = aNode.getNodeType();
	
		if(tipoNodo != Node.TEXT_NODE)
			return false;
		
		return true;
	}
   
	/**
	 * 
	 * @param aNode
	 */
	private void cercaMarker(Node aNode)
	{
		
		
		String text = aNode.getTextContent();
		/*
		System.out.println( 	"#Nome nodo: "+aNode.getNodeName() 
							+ 	", valore originale: "
							+	text 
							+	" | Valore sostituito: " 
							+ 	Marker.findMarker(text,null));
		*/
		Marker.findNodeMarker(aNode, liste);
			
	}
	
	public void save(String filename)
	{
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = null;
		try {
			transformer = transformerFactory.newTransformer();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
				
	
		Node pageArea = dom.getElementsByTagName("pageArea").item(0);
		for(FloatingField ff : liste.listaFloatingFields) 
		{	
			pageArea.appendChild(ff.getFieldElement(dom));	
		}
		
		DOMSource source = new DOMSource(dom);//doc
		StreamResult result = new StreamResult(new File(filename));
		try {
			transformer.transform(source, result);
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		
	

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		XDP_Reader reader = new XDP_Reader("prova.xdp");		
		//XDP_Reader reader = new XDP_Reader("simple.xdp");		
		reader.run();
		reader.save("simpleout.xdp");
		reader.liste.stampa_lista();
	}

}
